package cn.worken.baseframe.dubbo.impl.impl;

import cn.worken.baseframe.dubbo.facade.dto.DemoDTO;
import cn.worken.baseframe.dubbo.facade.dto.QueryDemoDTO;
import cn.worken.baseframe.dubbo.facade.facade.DIDemoFacade;
import cn.worken.baseframe.facade.bo.DemoBO;
import cn.worken.baseframe.facade.facade.IDemoFacade;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author genxin.liu
 * @since 2021/12/28 14:08
 */
@DubboService
public class DIDemoFacadeImpl implements DIDemoFacade {
    
    @Resource
    private IDemoFacade demoFacade;
    
    @Override
    public List<DemoDTO> queryDemoList(QueryDemoDTO queryDemoDTO) {
        List<DemoBO> demoBOList = demoFacade.queryDemoList(to(queryDemoDTO));
        return to(demoBOList);
    }
    
    public cn.worken.baseframe.bean.dto.QueryDemoDTO to(QueryDemoDTO bean) {
        cn.worken.baseframe.bean.dto.QueryDemoDTO data = new cn.worken.baseframe.bean.dto.QueryDemoDTO();
        data.setName(bean.getName());
        return data;
    }
    
    private List<DemoDTO> to(List<DemoBO> demoBOList) {
        if (CollectionUtils.isEmpty(demoBOList)) {
            return Collections.EMPTY_LIST;
        }
        return demoBOList.stream().map(demoBO -> {
            DemoDTO data = new DemoDTO();
            data.setId(demoBO.getId());
            data.setName(demoBO.getName());
            return data;
        }).collect(Collectors.toList());
    }
}
